//
//  MortgageInfoController.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/6/21.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON
class MortgageInfoController: BaseController {
    var appTitle = ""//导航title
    var orderType = ""//抵押贷款、二手车买卖
    var mortgage:MortgageModel!
    var minfoTable:UITableView!//数据表格
    var miCell:MortgageCell!
    override func viewDidLoad() {
        super.viewDidLoad()
        //初始化表格
        setupNavBar(title: appTitle, isback: true, appearWay: 3, isright: false)//自定义导航
        minfoTable = UITableView(frame: CGRect(x:0,y:kStatusBarH + kNavigationBarH,width:kScreenW,height:kScreenH - kStatusBarH - kNavigationBarH),style: UITableViewStyle.plain)
        minfoTable?.delegate = self
        minfoTable?.dataSource = self
        minfoTable?.separatorStyle = UITableViewCellSeparatorStyle.none
        minfoTable?.rowHeight = 10*cellH + 10
        self.view.addSubview(minfoTable)
    }
}
extension MortgageInfoController:UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        miCell = tableView.dequeueReusableCell(withIdentifier: "miCellID") as? MortgageCell
        if miCell == nil {
            miCell = MortgageCell(style: UITableViewCellStyle.default,reuseIdentifier: "miCellID")
        }
        miCell.carType.text = mortgage.carType
        miCell.mileage.text = mortgage.mileage
        miCell.cardCity.text = mortgage.cardCity
        miCell.cardTime.text = mortgage.cardTime
        miCell.carOwner.text = mortgage.carOwner
        miCell.ownerPhone.text = mortgage.ownerPhone
        miCell.remark.text = mortgage.remark
        miCell.carType.isEnabled = false
        miCell.mileage.isEnabled = false
        miCell.cardCity.isEnabled = false
        miCell.cardTime.isEnabled = false
        miCell.carOwner.isEnabled = false
        miCell.ownerPhone.isEnabled = false
        miCell.remark.isEditable = false
        if "1" == isEmployee {//普通用户登录
            var btnTitle = ""
            if "1" == mortgage.ostatus {
                btnTitle = "等待处理"
            }else if "2" == mortgage.ostatus {
                btnTitle = "处理中"
            }else if "3" == mortgage.ostatus {
                btnTitle = "已处理"
            }
            miCell.submitTradeBtn.setTitle(btnTitle, for: .normal)
            miCell.submitTradeBtn.backgroundColor = UIColor.lightGray
        }
        if "2" ==  isEmployee {//员工
            if "1" == mortgage.ostatus {
                miCell.submitTradeBtn.setTitle("抢单", for: .normal)
                miCell.submitTradeBtn.addTarget(self, action: #selector(acceptFun), for: .touchUpInside)
            }
            if "2" == mortgage.ostatus {
                miCell.submitTradeBtn.setTitle("完成", for: .normal)
                miCell.submitTradeBtn.addTarget(self, action: #selector(finishFun), for: .touchUpInside)
            }
            if "3" == mortgage.ostatus {
                 miCell.submitTradeBtn.setTitle("已完成", for: .normal)
                 miCell.submitTradeBtn.backgroundColor = UIColor.lightGray
            }
        }
        return miCell!
    }
}
extension MortgageInfoController{
    //抢单
    func acceptFun() {
        let urlString = "\(projectUrl)/order/other/accept/\(orderType)/\(mortgage.orderNo)/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
        toOperatOrder(url: urlString)
    }
    //完成
    func finishFun(){
        let urlString = "\(projectUrl)/order/other/finish/\(orderType)/\(mortgage.orderNo)/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
        toOperatOrder(url: urlString)
    }
}
